import pygame
import operator
import math
import towers.towerbase as TowerBase
from towers.projectile import Projectile
from helpers import load_image
from helpers import load_sound_fx

class Canon(TowerBase.TowerBase):
    
    player = None
    
    name = 'Canon Tower'
    fire_type = 'projectile'
    baseRadius = [200, 205, 210, 215, 220, 225, 230, 235, 240, 245]
    baseDamage = [50, 55, 60, 65, 70, 75, 80, 85, 90, 95]
    baseFireRate = [60, 58, 56, 54, 52, 50, 48, 46, 44, 42]
    baseCost = [25, 25, 25, 25, 25]
    
    base_energy_cost = [50, 90, 90, 90, 90, 90]
    base_mass_cost   = [50, 90, 90, 90, 90, 90]
    mass_cost   = 0
    energy_cost = 0
    base_shot_mass   = [25, 25, 25, 25, 25, 25]
    base_shot_energy = [2, 2, 2, 2, 2, 2]
    shot_mass   = 0
    shot_energy = 0    
    
    radius = 0
    damage = 0
    fireRate = 0
    rotationRate = 0.3
    cost = 0
    killCount = 0
    cooldown = 0
    projectile_list = None
    target = None
    user_lvl = 0
    exp_lvl = 0
    ttl = 0
    
    projectile_color = (255,255,0)
    
    def __init__(self, player, rect=None):
        TowerBase.TowerBase.__init__(self)
        self.player = player
        
        self.mass_cost = self.base_mass_cost[0]
        self.energy_cost = self.base_energy_cost[0]
        self.shot_mass = self.base_shot_mass[0]
        self.shot_energy = self.base_shot_energy[0]         
        
        # Load graphic for the tower
        pygame.sprite.Sprite.__init__(self)
        self.base_image, self.base_rect = load_image('tower_base.png', -1)
        self.image, self.rect = load_image('turret_canon.png', -1)
        self.orig_image = self.image
            
        # Projectile list
        self.projectile_list = pygame.sprite.Group()    
            
        # Update current stats
        self.radius = self.baseRadius[0]
        self.damage = self.baseDamage[0]
        self.fireRate = self.baseFireRate[0]
        self.cost = self.baseCost[0]
        
        # Load sound FX
        self.sound = load_sound_fx('canon_fire.wav')
    
    def get_projectile(self):
        return Projectile(self, self.target, 10)
        
        

    def on_hit(self, target):
        """Triggered when a projectile hits a target"""
        target.health -= self.damage
        if target.health <= 0:
            #self.player.money += target.reward
            target.kill()
            self.killCount += 1
        # AOE Code
        for aoe_target in self.ship_list:
            distance = math.sqrt((target.rect.centerx - aoe_target.rect.centerx)**2 + \
                      (target.rect.centery - aoe_target.rect.centery)**2)
            if distance <= 30 and not distance == 0:
                aoe_target.health -= self.damage
                if aoe_target.health <= 0:
                    #self.player.money += target.reward
                    aoe_target.kill()
                    self.killCount += 1